package tech.veedo.munchkin.controllers.backend;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import tech.veedo.munchkin.beans.basic.User;
import tech.veedo.munchkin.beans.backend.Workflow;
import tech.veedo.munchkin.services.backend.WorkflowService;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "1-后台-1-顶层工作流接口集合")
@RequestMapping("/munc/workflow")
public class WorkflowController {

    private final WorkflowService workflowService;

    public WorkflowController(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    @ApiOperation("新增顶层工作流接口")
    @PostMapping("/save")
    public boolean save(@RequestBody Workflow workflow) {
        User user = User.defaultU();
        log.info("now munchkin begin inserting workflow");
        return workflowService.saveWorkflow(workflow, user);
    }

    @ApiOperation("更新顶层工作流接口")
    @PostMapping("/update")
    public boolean update(@RequestBody Workflow workflow) {
        if (ObjectUtils.isEmpty(workflow.getId())) {
            throw new NullPointerException("当前要更新的顶层工作流id为空");
        }
        User user = User.defaultU();
        log.info("now munchkin begin updateing workflow, id:"+workflow.getId());
        return workflowService.updateWorkflow(workflow, user);
    }

    @ApiOperation("删除顶层工作流接口")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new NullPointerException("当前要更新的顶层工作流id为空");
        }
        return workflowService.deleteWorkflow(id, User.defaultU());
    }

    @ApiOperation(("查询顶层工作流列表"))
    @GetMapping("/list")
    public List<Workflow> list() {
        return workflowService.list();
    }

}
